﻿// <copyright file="Workset.cs" company="Horiba Medical">
//     Copyright (c) Horiba Medical. All rights reserved.
// </copyright>
// <summary>
//     Handling for file collections (sources and headers).
// </summary>

namespace DoxyOutParser.WorkingSet
{
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.IO;
	using System.Text.RegularExpressions;

	/// <summary>
	/// Provides a way of handling sources and header files collections.
	/// </summary>
	public class WorkSet
	{
		/// <summary>
		/// The project file related to this working set.
		/// </summary>
		private string projectFile;

		/// <summary>
		/// List of include directories found in the project file.
		/// </summary>
		private string[] includeDirectoriesTable;

		/// <summary>
		/// List of source files found in the project file.
		/// </summary>
		private List<string> sourceFilesTable;

		/// <summary>
		/// Root directory for this project file.
		/// </summary>
		private string rootDir;

		/// <summary>
		/// List of mandatory definitions for a proper Splint execution.
		/// </summary>
		private string[] definitionArray;

		/// <summary>
		/// Initializes a new instance of the WorkSet class.
		/// </summary>
		/// <param name="file">The project file to work on.</param>
		/// <param name="includeDirectories">A list of include directories found in the project file.</param>
		/// <param name="sourceFiles">A list of source files found in the project file.</param>
		/// <param name="definitions">A list of definitions for splint call.</param>
		public WorkSet(string file, string[] includeDirectories, List<string> sourceFiles, string[] definitions)
		{
			this.sourceFilesTable = sourceFiles;

			// Remove leading whitespaces in definitions
			for (int i = 0; i < definitions.Length; i++)
			{
				definitions[i] = definitions[i].TrimStart(' ');
			}

			this.definitionArray = definitions;
			this.projectFile = file;
			this.rootDir = Path.GetDirectoryName(file).ToString();
			this.includeDirectoriesTable = includeDirectories;
		}

		/// <summary>
		/// Gets the list of source files specified in the project file corresponding to this workset.
		/// </summary>
		/// <value>Source files found in the project file.</value>
		/// <returns>A list of source files.</returns>
		public string ProjectName
		{
			get
			{
				int startIndex = this.projectFile.LastIndexOf('\\') + 1;
				int endIndex = this.projectFile.LastIndexOf('.');
				return this.projectFile.Substring(startIndex, endIndex - startIndex);
			}
		}

		/// <summary>
		/// Gets the list of source files specified in the project file corresponding to this workset.
		/// </summary>
		/// <value>Source files found in the project file.</value>
		/// <returns>A list of source files.</returns>
		public List<string> SourceFiles
		{
			get
			{
				return this.sourceFilesTable;
			}
		}

		/// <summary>
		/// Gets or sets the root directory for to the project file corresponding to this workset.
		/// </summary>
		/// <value>Root directory for this project working set.</value>
		public string RootDir
		{
			get
			{
				return this.rootDir;
			}

			set
			{
				this.rootDir = value;
			}
		}

		/// <summary>
		/// Gets the array of include directories specified in the project file corresponding to this workset.
		/// </summary>
		/// <returns>An array of include files.</returns>
		public string[] GetIncludeDirectories()
		{
			return this.includeDirectoriesTable;
		}

		/// <summary>
		/// Gets the array of definitions specified in the project file corresponding to this workset.
		/// </summary>
		/// <returns>An array of definitions.</returns>
		public string[] GetDefinitions()
		{
			return this.definitionArray;
		}

		/// <summary>
		/// Returns the project file name corresponding to this workset.
		/// </summary>
		/// <returns>A string containing the project file name.</returns>
		public override string ToString()
		{
			return Path.GetFileName(this.projectFile).ToString();
		}
	}
}
